package com.opensky.module.sys.domain.jpa.entity;


import com.opensky.module.sys.domain.jpa.repository.SysRoleMenuRepository;
import com.opensky.module.sys.interfaces.frm.SysRoleMenuQueryFrm;
import com.opensky.common.infrastructure.base.jpa.BaseEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.criteria.Predicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * 角色菜单关联表(表sys_role_menu)ORM实体类
 *
 * @author 中国印
 * @date 2021-11-27 21:44:17
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamicUpdate
@DynamicInsert
@Table(name = "sys_role_menu")
public class SysRoleMenu extends BaseEntity<SysRoleMenuRepository, SysRoleMenu, Long> {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    @Column(name = "id", insertable = false, updatable = false, nullable = false, columnDefinition = "bigint(20) COMMENT '表主键'")
    private Long id;
    /**
     * 菜单ID
     */
    @Column(name = "menu_id")
    private Long menuId;
    /**
     * 角色ID
     */
    @Column(name = "role_id")
    private Long roleId;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;

    private static SysRoleMenuRepository repository() {
        return SysRoleMenu.Holder.REPO;
    }

    private static final class Holder {
        private static final SysRoleMenuRepository REPO = getRepository(SysRoleMenuRepository.class, SysRoleMenu.class);
    }

    /**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<SysRoleMenu> getSpecification(SysRoleMenuQueryFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (frm.getId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), frm.getId()));
            }
            if (frm.getMenuId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("menuId"), frm.getMenuId()));
            }
            if (frm.getRoleId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("roleId"), frm.getRoleId()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

}
